package com.pureut.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.equipment.domain.PatrolItem;
import com.pureut.equipment.domain.dto.PatrolItemDto;
import com.pureut.equipment.domain.dto.PatrolItemPlanDto;
import com.pureut.equipment.domain.vo.PatrolItemVo;
import com.pureut.equipment.mapper.PatrolItemMapper;
import com.pureut.equipment.mapper.PatrolItemPlanMapper;
import com.pureut.equipment.service.PatrolItemService;
import com.pureut.system.api.FeignService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/***
 *
 * Author:巡检项目实现层
 * Date:2023/04/26 9:46
 * @DESC
 */
@Service
public class PatrolItemServiceImpl extends ServiceImpl<PatrolItemMapper, PatrolItem> implements PatrolItemService {

    /**
     * 巡检项目mapper
     */
    @Resource
    PatrolItemMapper patrolItemMapper;

    /**
     * 基础模块fegin
     */
    @Resource
    FeignService feignService;

    /**
     * 巡检计划mapper
     */
    @Resource
    PatrolItemPlanMapper patrolItemPlanMapper;

    /**
     * 巡检项目列表
     *
     * @param patrolItemVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "patrol_item")
    public List<PatrolItemDto> getList(PatrolItemVo patrolItemVo) {
        return patrolItemMapper.getList(patrolItemVo);
    }

    /**
     * 新增
     *
     * @param patrolItemVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(PatrolItemVo patrolItemVo) throws Exception {
        PatrolItem patrolItem = new PatrolItem();
        String authorityCoding = feignService.getAuthorityCoding("device:inspection:project:list");
        if (authorityCoding == null) {
            throw new GlobalException("该单据未进行配置，无法生成单据编码");
        }
        patrolItem.setPatrolItemCode(authorityCoding)
                .setPatrolItemName(patrolItemVo.getPatrolItemName())
                .setWorkingHour(patrolItemVo.getWorkingHour())
                .setPatrolItemContent(patrolItemVo.getPatrolItemContent())
                .setInspectionStandard(patrolItemVo.getInspectionStandard())
                .setInspectionType(patrolItemVo.getInspectionType())
                .setDecisionResult(patrolItemVo.getDecisionResult())
                .setRemark(patrolItemVo.getRemark())
                .setCreateTime(new Date())
                .setCreateBy(SecurityUtils.getUsername())
                .setDeptId(SecurityUtils.getDeptId());
        return save(patrolItem);
    }

    /**
     * 修改
     *
     * @param patrolItemVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePatrolItem(PatrolItemVo patrolItemVo) {
        PatrolItem patrolItem = getById(patrolItemVo.getId());
        patrolItem.setPatrolItemName(patrolItemVo.getPatrolItemName())
                .setWorkingHour(patrolItemVo.getWorkingHour())
                .setPatrolItemContent(patrolItemVo.getPatrolItemContent())
                .setInspectionStandard(patrolItemVo.getInspectionStandard())
                .setInspectionType(patrolItemVo.getInspectionType())
                .setDecisionResult(patrolItemVo.getDecisionResult())
                .setRemark(patrolItemVo.getRemark());
        return updateById(patrolItem);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deletePatrolItem(Long id) {
        PatrolItem byId = getById(id);
        // 该项目已关联巡检计划，无法删除
        List<PatrolItemPlanDto> patrolItemPlanDtos = patrolItemPlanMapper.selectPatrolItem(id);
        if (patrolItemPlanDtos.size() > 0) {
           throw new GlobalException("该项目已经关联巡检计划，无法删除");
        }
        return removeById(byId);
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public PatrolItemDto queryPatrolItem(Long id) {


        return patrolItemMapper.getDataById(id);
    }
}
